package module

import (
	mysql "myapp/mysql"
	"myapp/structure"
	"net/http"
	"strings"

	_ "github.com/lib/pq"

	"github.com/labstack/echo"
)

// 注册
func Registra(c echo.Context) error {
	user := new(structure.User)
	error := structure.Error
	ok := structure.Ok

	if err := c.Bind(user); err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if user.Name == "" || user.Password == "" {
		error.Msg = "请输入账号密码"
		return c.JSON(http.StatusOK, error)
	}

	_, err := mysql.Query(strings.Join([]string{"INSERT INTO test(name,password) VALUES  ('", user.Name, "','", user.Password, "')"}, ""))

	if err != nil {
		error.Msg = "注册失败"
		return c.JSON(http.StatusOK, error)
	}

	ok.Msg = "注册成功"
	return c.JSON(http.StatusOK, ok)
}
